﻿using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace MyCommon
{
    /// <summary>
    /// 封装Framework自带的XmlSerializer，实现实体直接到XML字符串的序列化 --郭旭东
    /// </summary>
    [Obsolete("该类已转到MyCommon.Serialization命名空间下")]
    public class XmlStringSerializer
    {
        /// <summary>
        /// 构造XmlStringSerializer
        /// </summary>
        public XmlStringSerializer() { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rootAttr">指定序列化出的XML根元素的名称及属性</param>
        public XmlStringSerializer(XmlRootAttribute rootAttr)
        {
            RootAttrbute = rootAttr;
        }
        /// <summary>
        /// 重写Xml根元素的装饰属性
        /// </summary>
        public XmlRootAttribute RootAttrbute { get; private set; }

        XmlSerializer GetSerializer(Type type)
        {
            return RootAttrbute == null ? new XmlSerializer(type) : new XmlSerializer(type, RootAttrbute);
        }
        /// <summary>
        /// 将对象序列化为XML字符串
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns></returns>
        public string Serialize(object obj)
        {
            var ser = GetSerializer(obj.GetType());
            using (var writer = new MyStringWriter(Encoding.GetEncoding("GBK")))
            {
                ser.Serialize(writer, obj);
                return writer.ToString();
            }
        }
        /// <summary>
        /// 将XML字符串反序列化为对象
        /// </summary>
        /// <typeparam name="T">返回对象的类型</typeparam>
        /// <param name="input">要反序列化的XML字符串</param>
        /// <returns></returns>
        public T Deserialize<T>(string input) where T : class
        {
            var ser = GetSerializer(typeof(T));
            using (var reader = new StringReader(input))
            {
                return ser.Deserialize(reader) as T;
            }
        }

        class MyStringWriter : StringWriter
        {
            Encoding m_encoding;
            public MyStringWriter() { }
            public MyStringWriter(IFormatProvider formatProvider) : base(formatProvider) { }
            public MyStringWriter(StringBuilder sb) : base(sb) { }
            public MyStringWriter(StringBuilder sb, IFormatProvider formatProvider) : base(sb, formatProvider) { }

            public MyStringWriter(Encoding encoding) { m_encoding = encoding; }
            public MyStringWriter(Encoding encoding, StringBuilder sb) : base(sb) { m_encoding = encoding; }
            public MyStringWriter(Encoding encoding, IFormatProvider formatProvider) : base(formatProvider) { m_encoding = encoding; }
            public MyStringWriter(Encoding encoding, StringBuilder sb, IFormatProvider formatProvider) : base(sb, formatProvider) { m_encoding = encoding; }

            public override Encoding Encoding
            {
                get
                {
                    if (m_encoding == null)
                    {
                        return base.Encoding;
                    }
                    return m_encoding;
                }
            }
        }
    }
}
